//
//  KLPhotoBrowserViewController.swift
//  BCWeiBo
//
//  Created by bcmac3 on 16/4/5.
//  Copyright © 2016年 KellenYangs. All rights reserved.
//

import UIKit

let KLPhotoBrowserReuseIdentifier = "KLReuseIdentifier"

class KLPhotoBrowserViewController: UIViewController {

    /// 图片数组
    var imageURLs: [NSURL]
    /// 用户选中的图片索引
    var currentIndex: Int
    
    init(urls:[NSURL], index: Int)
    {
        imageURLs = urls
        currentIndex = index
        
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        // 1.初始化UI
        setupUI()
    }
    
    private func setupUI() {
        // 1. 添加控件
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        
        // 2. 设置控件位置
        
        
        closeButton.xmg_AlignInner(type: XMG_AlignType.BottomLeft, referView: view, size: CGSize(width: 100, height: 32), offset: CGPoint(x: 8, y: -8))
        
        saveButton.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: view, size: CGSize(width: 100, height: 32), offset: CGPoint(x: -8, y: -8))
        
        collectionView.frame = UIScreen.mainScreen().bounds
        
        // 3. 监听方法
        saveButton.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        closeButton.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 4.注册重用cell设置数据源
        collectionView.dataSource = self
        collectionView.registerClass(KLPhotoBrowserCell.self, forCellWithReuseIdentifier: KLPhotoBrowserReuseIdentifier)
    }
    // MARK: - 按钮监听方法
    /// 关闭
    func close() {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    /// 保存
    func save() {
        // TODO: 保存照片
        print(__FUNCTION__)
    }
    
    
    // MARK: - 懒加载
    private lazy var closeButton: UIButton = UIButton(title: "关闭", fontSize: 14, color: UIColor.whiteColor(), backColor: UIColor.darkGrayColor())
    private lazy var saveButton: UIButton = UIButton(title: "保存", fontSize: 14, color: UIColor.whiteColor(), backColor: UIColor.darkGrayColor())
    
    private lazy var collectionView:UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: PhotoBrowserLayout())
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class PhotoBrowserLayout: UICollectionViewFlowLayout {
    override func prepareLayout() {
        
        // 设置布局
        itemSize = UIScreen.mainScreen().bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView!.pagingEnabled = true
        collectionView!.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
}

extension KLPhotoBrowserViewController: UICollectionViewDataSource, KLPhotoBrowserCellDelegate
{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(KLPhotoBrowserReuseIdentifier, forIndexPath: indexPath) as! KLPhotoBrowserCell
        cell.photoDelegate = self
        
        cell.backgroundColor = UIColor.blackColor()
        cell.imageURL = imageURLs[indexPath.item]
        
        return cell
    }
    // MARK: - PhotoBrowserCellDelegate
    func photoBrowserCellDidTapImage()
    {
        // 关闭浏览器
        close()
    }

}
